<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="${package.Mapper}.${table.mapperName}">

<#if enableCache>
    <!-- 开启二级缓存 -->
    <cache type="org.mybatis.caches.ehcache.LoggingEhcache"/>

</#if>
<#if baseResultMap>
    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="${package.Entity}.${entity}">
<#list table.fields as field>
<#if field.keyFlag><#--生成主键排在第一位-->
        <id column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
<#list table.commonFields as field><#--生成公共字段 -->
    <result column="${field.name}" property="${field.propertyName}" />
</#list>
<#list table.fields as field>
<#if !field.keyFlag><#--生成普通字段 -->
        <result column="${field.name}" property="${field.propertyName}" />
</#if>
</#list>
    </resultMap>

</#if>
<#if baseColumnList>
    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
<#list table.commonFields as field>
        ${field.name},
</#list>
        ${table.fieldNames}
    </sql>

</#if>

    <sql id="Assist">
        <where>
            <foreach collection="require" item="req" separator=" ">
                ${r'${req.require}'}
                <if test="req.value != null"> ${r'#{req.value}'}</if>
                <if test="req.values != null">
                    <foreach collection="req.values" item="val" separator=",">
                        ${r'#{val}'}
                    </foreach>
                </if>
                <if test="req.suffix != null"> ${r'${req.suffix}'}</if>
            </foreach>
        </where>
    </sql>

    <select id="selectList_commom" parameterType="com.liqi.frame.comm.mybatisplus.Assist" resultType="${package.Entity}.${entity}">
        select
        <if test="distinct !=null">${r'${distinct}'}</if>
        <choose>
            <when test="resultColumn!=null">${r'${resultColumn}'}</when>
            <otherwise>
                *
            </otherwise>
        </choose>
        from ${table.name}
        <if test="require!=null">
            <include refid="Assist" />
        </if>
        <if test="order !=null">${r'${order}'}</if>
    </select>

    <select id="selectById_common" parameterType="String" resultType="${package.Entity}.${entity}">
        select *
        from ${table.name}
        where id = ${r'#{id}'}
    </select>

    <insert id="insertObject_common">
        INSERT ignore
        INTO ${table.name} (
        <#list table.commonFields as field>
            <#if field_index==0>
            ${field.name}
            </#if>
            <#if field_index!=0>
            ,${field.name}
            </#if>
        </#list>
        <#list table.fields as field>
            ,${field.name}
        </#list>
        )
        VALUES
        (
        <#list table.commonFields as field>
            <#if field_index==0>
            ${r'#{'}${field.propertyName}${r'}'}
            </#if>
            <#if field_index!=0>
            ,${r'#{'}${field.propertyName}${r'}'}
            </#if>
        </#list>
        <#list table.fields as field>
            ,${r'#{'}${field.propertyName}${r'}'}
        </#list>
        )
    </insert>

    <update id="updateObject_common">
        UPDATE ${table.name}
        <set>
            <#list table.commonFields as field>
            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                ${field.name} = ${r'#{'}${field.propertyName}${r'}'},
            </if>
            </#list>
            <#list table.fields as field>
            <if test="${field.propertyName}!=null and ${field.propertyName}!=''">
                ${field.name} = ${r'#{'}${field.propertyName}${r'}'},
            </if>
            </#list>
        </set>
        WHERE id = ${r'#{id}'}
    </update>

    <delete id="deleteById_common">
        DELETE FROM ${table.name}
        WHERE id = ${r'#{id}'}
    </delete>
</mapper>
